package com.zxshhy.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: dengrd
 * @Date: 2018/9/14
 * @Description:
 */
@Slf4j
@Configuration
@RefreshScope
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String redisAddress;

    @Value("${spring.redis.port}")
    private int redisPort;
    @Value("${spring.redis.password:}")
    private String password;

    @Value("${spring.redis.pingConnectionInterval:1000}")
    private int pingConnectionInterval;

    @Bean
    public RedissonClient redissonClient(){
        log.info("\nRedis连接信息：\nhost：{}\nport：{}\npassword：{}",redisAddress,redisPort,password);
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://"+redisAddress+":"+redisPort).setPingConnectionInterval(pingConnectionInterval);
        if(StringUtils.isNotBlank(password)) singleServerConfig.setPassword(password);
        return Redisson.create(config);
    }
}
